import {SHARE_VIDEO_SORT_TYPE, ShareVideoRankDetail} from "../../engines/services/miniGameContent/miniApp";
import O1HD from "../../engines/gameEngine";
import shareVideoItem from "./shareVideoRank/shareVideoItem";

const {ccclass, property} = cc._decorator;

@ccclass
export default class shareVideoRank extends cc.Component {

    @property(cc.Prefab)
    pbItem: cc.Prefab = null;
    @property(cc.ToggleContainer)
    tcType: cc.ToggleContainer = null;
    @property(cc.Node)
    ndListContent: cc.Node = null;

    onLoad() {
        this.OnSelectType(SHARE_VIDEO_SORT_TYPE.HOT);
    }

    start() {
    }

    OnBtnSwitch(target: cc.Toggle) {
        let num: number = Number(target.node.name);
        this.OnSelectType(num);
    }

    OnBtnExit() {
        this.ndListContent.destroyAllChildren();
        this.node.destroy();
    }

    OnSelectType(type: SHARE_VIDEO_SORT_TYPE) {
        O1HD.getModules().uiControlModule.showLoading();
        O1HD.getServices().miniGameSrv.getAppApiInstance().getShareVideoRank(type, data => {
            this.OnShowList(data);
            O1HD.getModules().uiControlModule.hideLoading();
        })
    }

    OnShowList(data: ShareVideoRankDetail[]) {
        this.ndListContent.destroyAllChildren();
        this.scheduleOnce(() => {
            //
            for (let info of data) {
                let node = cc.instantiate(this.pbItem);
                this.ndListContent.addChild(node);
                node.getComponent(shareVideoItem).BindVideoData(info);
            }
        })
    }

    // update (dt) {}
}
